package a3lauer;


import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

public class EllipseListGUI extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private final String sortAreaString = "Area";
	private final String sortMinAxisString = "Minor Axis";
	private final String sortMajAxisString = "Major Axis";
	private final String sortPariString = "Parimeter";

	private JList listview;
	private JScrollPane scroll;
	private JButton sortArea, sortMinAxis, sortMajAxis, sortPari;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem openMenu, saveMenu, newMenu;
	private JFileChooser fc;

	private DefaultListModel listmodel;

	private final int frameX = 100, frameY = 100, frameW = 450, frameH = 335;
	private final int scrollX = 30, scrollY = 30, scrollW = 255, scrollH = 200;
	private final int buttonX = 300, buttonY = 50, buttonW = 95, buttonH = 30;

	// Creating the GUI
	public EllipseListGUI(String title) {
		setBounds(frameX, frameY, frameW, frameH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(null);

		// sets up a default of 22 ellipses with random numbers between 1 and 50
		listmodel = new DefaultListModel();
		for (int i = 0; i < 22; i++) {
			double randomNum1 = Math.random() * 50 + 1;
			double randomNum2 = Math.random() * 50 + 1;
			double randomNum3 = Math.random() * 50 + 1;
			double randomNum4 = Math.random() * 50 + 1;
			Ellipse ellipse = new Ellipse((int) randomNum1, (int) randomNum2,
					(int) randomNum3, (int) randomNum4, (i + 1));
			listmodel.addElement(ellipse);
		}

		// add listmodel and scroll bars to window
		listview = new JList(listmodel);
		listview.setVisible(true);
		scroll = new JScrollPane(listview);
		scroll.setBounds(scrollX, scrollY, scrollW, scrollH);
		add(scroll);

		sortMajAxis = new JButton(sortMajAxisString);
		sortMinAxis = new JButton(sortMinAxisString);
		sortPari = new JButton(sortPariString);
		sortArea = new JButton(sortAreaString);

		sortMajAxis.addActionListener(this);
		sortMinAxis.addActionListener(this);
		sortPari.addActionListener(this);
		sortArea.addActionListener(this);

		// set buttons to be on right side of window
		sortMajAxis.setBounds(buttonX, buttonY, buttonW, buttonH);
		sortMinAxis
				.setBounds(buttonX, buttonY + buttonH + 10, buttonW, buttonH);
		sortPari.setBounds(buttonX, buttonY + buttonH * 2 + 20, buttonW,
				buttonH);
		sortArea.setBounds(buttonX, buttonY + buttonH * 3 + 30, buttonW,
				buttonH);

		add(sortArea);
		add(sortMinAxis);
		add(sortMajAxis);
		add(sortPari);

		// drop down menus
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		menu = new JMenu("File");
		menuBar.add(menu);

		newMenu = new JMenuItem("New");
		newMenu.addActionListener(this);
		menu.add(newMenu);

		openMenu = new JMenuItem("Open");
		openMenu.addActionListener(this);
		menu.add(openMenu);

		saveMenu = new JMenuItem("Save");
		saveMenu.addActionListener(this);
		menu.add(saveMenu);

		// labels
		Label majAxLabel = new Label("MajAxis");
		majAxLabel.setBounds(100, 9, 35, 30);
		add(majAxLabel);

		Label minAxLabel = new Label("MinAxis");
		minAxLabel.setBounds(140, 9, 35, 30);
		add(minAxLabel);

		Label pariLabel = new Label("Perimeter");
		pariLabel.setBounds(185, 9, 30, 30);
		add(pariLabel);

		Label areaLabel = new Label("Area");
		areaLabel.setBounds(230, 9, 30, 30);
		add(areaLabel);
		setVisible(true);

	}

	@SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();

		if (source.equals(newMenu)) {
			listmodel.removeAllElements();
			for (int i = 0; i < 22; i++) {
				double randomNum1 = Math.random() * 50 + 1;
				double randomNum2 = Math.random() * 50 + 1;
				double randomNum3 = Math.random() * 50 + 1;
				double randomNum4 = Math.random() * 50 + 1;

				Ellipse ellipse = new Ellipse((int) randomNum1,
						(int) randomNum2, (int) randomNum3, (int) randomNum4,
						(i + 1));
				listmodel.addElement(ellipse);
			}
			listmodel.trimToSize();

			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));
		}

		if (source.equals(openMenu)) {
			System.out.println("Open");
					
			try {
				FileInputStream fileIn = new FileInputStream(new File("Untitled"));
				ObjectInputStream is= new ObjectInputStream(fileIn);
				source =  (ArrayList<Ellipse>) is.readObject();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {	
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			
		}

		if (source.equals(saveMenu)) {
			System.out.println("Save");

			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));

			int result = fc.showSaveDialog(this);
			if (result == JFileChooser.APPROVE_OPTION) {
			try{
				FileOutputStream fileStream = new FileOutputStream("Untitled");
				ObjectOutputStream os = new  ObjectOutputStream(fileStream);
				os.writeObject(listmodel);
				os.close();
			}catch(Exception ex){
				ex.printStackTrace();
			}
			}
		}

		if (source.equals(sortArea)) {
			// trims the listmodel down to size 
			listmodel.trimToSize();

			// adds what iss in the listmodel to the ArrayList
			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));

			Collections.sort(newList, new compareArea());

			listmodel.removeAllElements();

			// adds the sorted items back into the listmodel
			for (Ellipse s : newList)
				listmodel.addElement(s);

		}

		if (source.equals(sortMinAxis)) {
			// trims the listmodel down to size 
			listmodel.trimToSize();

			// adds what is in the list model to the ArrayList
			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));

			Collections.sort(newList, new compareMinAx());

			listmodel.removeAllElements();

			// adds the sorted items back into the listmodel
			for (Ellipse s : newList)
				listmodel.addElement(s);
		}

		if (source.equals(sortMajAxis)) {
			// trims the listmodel down to size 
			listmodel.trimToSize();

			// adds what is in the list model to the ArrayList
			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));

			Collections.sort(newList, new compareMajAx());

			listmodel.removeAllElements();

			// adds the sorted items back into the listmodel
			for (Ellipse s : newList)
				listmodel.addElement(s);
		}

		if (source.equals(sortPari)) {
			// trims the listmodel down to size 
			listmodel.trimToSize();

			// adds what is in the list model to the ArrayList
			ArrayList<Ellipse> newList = new ArrayList<Ellipse>();
			for (int i = 0; i < listmodel.capacity(); i++)
				newList.add((Ellipse) listmodel.elementAt(i));

			Collections.sort(newList, new comparePeri());

			listmodel.removeAllElements();

			// adds the sorted items back into the listmodel
			for (Ellipse s : newList)
				listmodel.addElement(s);
		}
	}

	// main method
	public static void main(String[] args) {
		new EllipseListGUI("Ellipse List");
	}

	class Ellipse implements Serializable {

		private static final long serialVersionUID = 1L;
		int ellipseNum;
		Integer area, minorAxis, majorAxis, perimeter;

		public Ellipse(Integer majAxisIn, Integer minAxisIn, Integer perIn,
				Integer areaIn, int ellipseNumIn) {
			majorAxis = majAxisIn;
			minorAxis = minAxisIn;
			perimeter = perIn;
			area = areaIn;
			ellipseNum = ellipseNumIn;
		}

		public String toString() {
			return "Ellipse " + ellipseNum + ":          " + majorAxis
					+ "          " + minorAxis + "          " + perimeter
					+ "          " + area;
		}

		public Integer getMajAx() {
			return majorAxis;
		}

		public Integer getMinAx() {
			return minorAxis;
		}

		public Integer getPerimeter() {
			return perimeter;
		}

		public Integer getArea() {
			return area;
		}
	}

	public class compareArea implements Comparator<Ellipse> {
		public int compare(Ellipse o1, Ellipse o2) {
			return o1.getArea().compareTo(o2.getArea());
		}
	}

	public class comparePeri implements Comparator<Ellipse> {
		public int compare(Ellipse o1, Ellipse o2) {
			return o1.getPerimeter().compareTo(o2.getPerimeter());
		}
	}

	public class compareMajAx implements Comparator<Ellipse> {
		public int compare(Ellipse o1, Ellipse o2) {
			return o1.getMajAx().compareTo(o2.getMajAx());
		}
	}

	public class compareMinAx implements Comparator<Ellipse> {
		public int compare(Ellipse o1, Ellipse o2) {
			return o1.getMinAx().compareTo(o2.getMinAx());
		}
	}
}
